package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ReservationServiceImpl implements ReservationService {
    private final ReservationMapper reservationMapper;

    @Override
    public Integer cancelReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        LocalDateTime startTime = LocalDate.now().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(24);

        int count = reservationMapper.countCancelledReservationsWithinTimeRange(startTime, endTime, userId);
        return count;
    }
    @Override
    public List<TimeCountVo> countReservationsForEachTimeWithinTimeRange(Long time) {
        LocalDateTime startTime = Instant.ofEpochMilli(time).atZone(ZoneOffset.ofHours(8)).toLocalDateTime().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = startTime.plusDays(24);
        return reservationMapper.countReservationsForEachTimeWithinTimeRange(startTime, endTime);
    }

    @Override
    public void insert(ReservationDto reservationDto) {
        Reservation reservation = BeanUtil.copyProperties(reservationDto, Reservation.class);

 /*       LocalDateTime startTime = LocalDate.now().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(24);
        //查询取消预约次数是否超过限制
        int count = reservationMapper.countCancelledReservationsWithinTimeRange(startTime, endTime, UserThreadLocal.getUserId());*/

        Integer count = cancelReservationCount();
        if ( count>= 3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        Reservation reservation1=reservationMapper.reservated(reservation.getMobile(), reservation.getTime(),reservation.getName());
        if (ObjectUtil.isNotEmpty(reservation1)){
            throw new BaseException(BasicEnum.TIME_ALREADY_RESERVATED_BY_PHONE);
        }
        reservation.setStatus(0);
        reservationMapper.insert(reservation);
    }

    @Override
    public PageResponse<ReservationVo> findByPage(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Reservation> reservationPage = reservationMapper.findByPage(pageNum, pageSize, null, null, status, null, null, null, null);
        return PageResponse.of(reservationPage, ReservationVo.class);
    }

    @Override
    public void cancelReservation(Long id) {
        Reservation reservation = reservationMapper.findById(id);
        reservation.setStatus(2);
        reservationMapper.update(reservation);
    }
}
